package framed;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.server.ServerContext;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServerEventHandler;
import org.apache.thrift.server.TThreadedSelectorServer;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TNonblockingServerTransport;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

// Generated code
import tutorial.Calculator;

public class JavaServer {

    public static final Logger LOG = LoggerFactory.getLogger(JavaServer.class);
    
    public static CalculatorHandler handler;

    public static Calculator.Processor processor;

    public static void main(String[] args) {
        try {
            handler = new CalculatorHandler();
            processor = new Calculator.Processor(handler);

            Runnable simple = new Runnable() {
                public void run() {
                    simple1(processor);
                }
            };

            new Thread(simple).start();
        } catch (Exception x) {
            x.printStackTrace();
        }
    }

    public static void simple(Calculator.Processor processor) {
        try {
            TNonblockingServerTransport transport = 
                    new TNonblockingServerSocket(9090);
            TServer server = new TNonblockingServer(
                    new TNonblockingServer.Args(transport).processor(processor));
            
            server.setServerEventHandler(new TestServerEvent());
            
            // Use this for a multithreaded server
            // TServer server = new TThreadPoolServer(new
            // TThreadPoolServer.Args(serverTransport).processor(processor));

            System.out.println("Starting the nonblocking server...");
            server.serve();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    
    public static void simple1(Calculator.Processor processor) {
        TNonblockingServerTransport transport;
        try {
            transport = new TNonblockingServerSocket(9090);
            
            TThreadedSelectorServer.Args args = new TThreadedSelectorServer.Args(transport).processor(
                    processor).workerThreads(10);
            
            // The maximum amount of memory we will allocate to client IO buffers at a time.
            // set 1MB.
            args.maxReadBufferBytes = 1024 * 1024L;
            
            TServer server = new TThreadedSelectorServer(args);

            LOG.info("Starting server.");
            server.serve();
            
        } catch (TTransportException e) {
            LOG.error(e.getMessage(), e);
        }
    }
    
    
    static class TestServerEvent implements TServerEventHandler {

        @Override
        public void preServe() {
            System.out.println("preServe...");
        }

        @Override
        public ServerContext createContext(TProtocol input, TProtocol output) {
            System.out.println("createContext..." + input.getTransport().getClass().getName());
            return null;
        }

        @Override
        public void deleteContext(ServerContext serverContext, TProtocol input,
                TProtocol output) {
            System.out.println("deleteContext...");
        }

        @Override
        public void processContext(ServerContext serverContext,
                TTransport inputTransport, TTransport outputTransport) {
            System.out.println("processContext...");
            
        }
        
    }

}
